package org.xfy.ymcc.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.util.StringUtils;
import org.xfy.common.jwt.JwtUtils;
import org.xfy.common.util.AssertUtil;
import org.xfy.ymcc.domain.CourseDoc;
import org.xfy.ymcc.domain.Login;
import org.xfy.ymcc.dto.AddCourseDto;
import org.xfy.ymcc.feign.SearchFeignClient;
import org.xfy.ymcc.service.ICourseService;
import org.xfy.ymcc.domain.Course;
import org.xfy.ymcc.query.CourseQuery;
import org.xfy.common.result.JsonResult;
import org.xfy.common.result.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;
    @Autowired
    private SearchFeignClient searchFeignClient;

    @PostMapping("/info")
    public JsonResult info(@RequestBody List<Long> courseIds){
        JsonResult course = searchFeignClient.getCourse(courseIds);
        Object data = course.getData();
        List<CourseDoc> courseDocs = JSON.parseArray(JSON.toJSONString(data), CourseDoc.class);
        return JsonResult.success().setData(courseDocs);
    }

    /**
     * 获取详情信息
     * @param courseId
     * @return
     */
    @GetMapping("/detail/data/{courseId}")
    public JsonResult getDetailData(@PathVariable("courseId")Long courseId){
        return courseService.getDetailData(courseId);
    }


    /**
     * 课程上线
     * @param courseIds
     * @return
     */
    @PostMapping("/onLineCourse")
    public JsonResult onLineCourse(@RequestBody List<Long> courseIds){
        return courseService.onLineCourse(courseIds);
    }

    /**
     * 查询回显数据
     * @param id
     * @return
     */
    @GetMapping("/getEdit/{id}")
    public JsonResult getEdit(@PathVariable("id")Long id){
        return courseService.getEdit(id);
    }

    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JsonResult saveOrUpdate(@RequestBody AddCourseDto addCourseDto, HttpServletRequest request){
        String header = request.getHeader("U-TOKEN");
        Object userInfo = JwtUtils.getUserInfo(header);
        Login login = JSON.parseObject(userInfo.toString(), Login.class);
        AssertUtil.isNotNull(addCourseDto.getCourse(),"课程不能为空");
        if(addCourseDto.getCourse().getId()!=null){
            courseService.updateById(addCourseDto,login.getId(),login.getUsername());
        }else{
            courseService.save(addCourseDto,login.getId(),login.getUsername());
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        courseService.removeById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success().setData(courseService.getById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JsonResult list(){
        return JsonResult.success().setData(courseService.list(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JsonResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPageNo(), query.getPageSize());
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(query.getKeyword()),Course::getName,query.getKeyword());
        queryWrapper.orderByDesc(Course::getId);
        page = courseService.page(page,queryWrapper);
        return JsonResult.success().setData(new PageList<Course>(page.getTotal(), page.getRecords()));
    }
}
